class User
  include MongoMapper::Document
  validates :email, :presence => true, :uniqueness => true
  validates :password, :confirmation => true
  attr_accessor :password
  attr_accessor :password_confirmation
  key :name, String
  key :email, String
  key :password, String
  key :garden_relation, Array
  many :gardens, :in => :garden_relation

  def User.authenticate(email, password)
    if users = User.all(:email => email)
      user = users.pop();
      if user==nil
          return user
      end
      if user.password == password
         return user
      end
    end
  end

  def add_garden (garden)
      if @gardens == nil
        @gardens = []
        @garden_relation = []
      end
      @gardens << garden
      @garden_relation << garden.id
  end
  def User.current()
    session[:user_id] || find_by_id(session[:user_id])
  end
end
